package ren.kane.phoenix.thread;

/**
 * @author renrk(1275) on 2019-08-15 10:20 AM
 **/
public class WaitTest {
    public static void main(String[] args) {
        Thread t = new ThreadA("t");
        synchronized (t) {
            try {
                // 启动“线程t”
                System.out.println(Thread.currentThread().getName() + " start t");
                t.start();

                // 主线程等待t通过notify()唤醒
                System.out.println(Thread.currentThread().getName() + " wait()");
                t.wait();

                System.out.println(Thread.currentThread().getName() + " continue");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class ThreadA extends Thread {
    public ThreadA(String name) {
        super(name);
    }

    @Override
    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + " call notify()");
            // 唤醒当前的wait线程
            notify();
        }
    }
}